/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazMatricula.java
 *
 * Created on 27/10/2011, 04:50:54 PM
 */

package presentacion;

import Paneles.PanelGeneral;
import business.EstudianteBO;
import business.GradoBo;
import dominio.Estudiante;
import java.awt.BorderLayout;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.ImageIcon;
/**
 *
 * @author SEBASTIAN
 */
public class InterfazMatricula extends javax.swing.JFrame {

    EstudianteBO estudiantebo;
    private GradoBo gradobo;
    private ImageIcon icono;

    private void inicializarTablaDeEstudiantes(List<Estudiante> listaentrada){
        int numfilas = listaentrada.size();

       jTable1.setModel(new DefaultTableModel(
            new Object[numfilas][5],
            new String [] {
                "Documento", "Apellido", "Nombre", "Fecha de inscripcion", "Grado al que aspira", "Matriculado"
            }));


        for (int indicefilas = 0; indicefilas < numfilas; indicefilas++) {
            this.jTable1.setValueAt(listaentrada.get(indicefilas).getDocEstudiante(),indicefilas,0);
            this.jTable1.setValueAt(listaentrada.get(indicefilas).getApellidoEst(),indicefilas,1);
            this.jTable1.setValueAt(listaentrada.get(indicefilas).getNombreEst(),indicefilas,2);
            this.jTable1.setValueAt(listaentrada.get(indicefilas).getFechaPreinscripcion(),indicefilas,3);
            this.jTable1.setValueAt(listaentrada.get(indicefilas).getIdGrado(),indicefilas,4);
            if(listaentrada.get(indicefilas).getMatriculado()==0)
                this.jTable1.setValueAt("No",indicefilas,5);
            else if(listaentrada.get(indicefilas).getMatriculado()==1)
                this.jTable1.setValueAt("Si",indicefilas,5);
        }

        gradobo=new GradoBo();
        estudiantebo=new EstudianteBO();
        Vector vecgrados=new Vector(gradobo.consultarGrados());
        this.jComboBox1.setModel(new DefaultComboBoxModel(vecgrados));
    }
    /** Creates new form InterfazMatricula */
    public InterfazMatricula() {
        initComponents();
        PanelGeneral panel=new PanelGeneral();
        this.add(panel,BorderLayout.SOUTH);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());

        TableColumn  column = null;

         /*Cambio el ancho de las columnas de la tabla jTable1*/
        column = this.jTable1.getColumnModel().getColumn(0);
        column.setPreferredWidth(85);
        column = this.jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(100);
        column = this.jTable1.getColumnModel().getColumn(2);
        column.setPreferredWidth(100);
        column = this.jTable1.getColumnModel().getColumn(3);
        column.setPreferredWidth(150);
        column = this.jTable1.getColumnModel().getColumn(4);
        column.setPreferredWidth(70);
        column = this.jTable1.getColumnModel().getColumn(5);
        column.setPreferredWidth(30);
        estudiantebo =new EstudianteBO();
        this.inicializarTablaDeEstudiantes(estudiantebo.obtenerListadoInscritos());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton4 = new javax.swing.JButton();

        setTitle("Matricular estudiantes");
        setResizable(false);

        jLabel1.setText("Marque con una \"S\" o con la palabra \"si\" los estudiante que formalizaron su matricula:");

        jTable1.setBackground(new java.awt.Color(102, 102, 255));
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Documento", "Apellido", "Nombre", "Fecha de inscripcion", "Grado al que esta inscrito", "Matriculado"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(170);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(300);
        jTable1.getColumnModel().getColumn(2).setPreferredWidth(80);

        jButton1.setText("Matricular estudiantes seleccionados");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Deshacer cambios");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(102, 102, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos de busqueda", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 153))); // NOI18N

        jButton3.setText("Filtrar busqueda");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText("Seleccione un grado al que se asignaron los estudiantes:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton3)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton4.setText("Cancelar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 342, Short.MAX_VALUE)
                        .addComponent(jButton4))
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String temp = null;
        for(int i=0;i<this.jTable1.getRowCount();i++)
        {
            this.jTable1.setValueAt(String.valueOf(this.jTable1.getValueAt(i, 5)).toLowerCase(), i, 5); //conversion a minusculas
        }
        int numatriculados=estudiantebo.actualizarAMatriculados(jTable1);
        ;
        JOptionPane.showMessageDialog(this,
                                      "Se han inscrito " +numatriculados+
                                      " Estudiantes",
                                      "Operacion exitosa",
                                      JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int opcion=JOptionPane.showConfirmDialog(this,
                                      "Esta opcion solo revertira cambios de estudiantes no matriculados ¿desea continuar?",
                                      "Advertencia",
                                      JOptionPane.YES_NO_OPTION);
        if(opcion==JOptionPane.YES_OPTION)
            this.inicializarTablaDeEstudiantes(estudiantebo.obtenerListadoInscritos());
        else if(opcion==JOptionPane.NO_OPTION);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String temp=new String(this.jComboBox1.getSelectedItem().toString());
        if(!estudiantebo.encontrarEstudiantesPorGradoInscritos(temp).isEmpty()){
            this.inicializarTablaDeEstudiantes((List<Estudiante>) estudiantebo.encontrarEstudiantesPorGradoInscritos(temp));
        }
        else{
            JOptionPane.showMessageDialog(this,
                                      "No se han encontrado estudiantes matriculados que aspiren al grado:\n\n" +
                                      temp,
                                      "Error",
                                      JOptionPane.OK_OPTION);
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new InterfazMatricula().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
